<?php
namespace Hawk\Model;
use Think\Model;
use Think\Model\RelationModel; 
class CustomerModel extends RelationModel 
{
	protected $insertFields = array('odd_numbers','cusname','sex','ID_number','product_is_use','age','tel','wechat','address','resource','financing','product_name','order_money','add_money','add_money_time','server_time_start','server_time_end','out_bank','inner_bank','part','group','sale_worker','order_remark','sale_head','sale_manager','is_checked','business_manager','businesser','server_worker','server_manager','customer_type','remark','receipt','status','redate','addtime','updatetime');
	protected $updateFields = array('id','odd_numbers','cusname','sex','ID_number','product_is_use','age','tel','wechat','address','resource','financing','product_name','order_money','add_money','add_money_time','server_time_start','server_time_end','out_bank','inner_bank','part','group','sale_worker','order_remark','sale_head','sale_manager','is_checked','business_manager','businesser','server_worker','server_manager','customer_type','remark','receipt','status','redate','addtime','updatetime');
	protected $_validate = array(
		array('cusname', 'require', '客户姓名不能为空！', 1, 'regex', 3),
		array('cusname', 'require', '客户姓名必须唯一！', 1, 'unique', 3),
		//array('ID_number', 'require', '身份ID不能为空！', 1, 'regex', 3),
		array('ID_number', 'require', '身份ID已存在！请核实。', 1, 'unique', 3),
		array('resource', 'require', '资源来源不能为空！', 1, 'regex', 1),
		// array('financing', 'require', '资金量不能为空！', 1, 'regex', 1),
		array('financing','1,15','资金格式不准确',3,'length'), 
		array('product_name', 'require', '产品名称不能为空！', 1, 'regex', 1),
		array('sale_head', 'require', '销售主任不能为空！', 1, 'regex', 1),
		array('sale_manager', 'require', '销售经理不能为空！', 1, 'regex', 1),
		array('customer_type', 'require', '客户分类不能为空！', 1, 'regex', 1),
	);
	public function _before_insert(&$data, $option){
		$data['addtime']=time();
		$data['updatetime']=time();
		$data['sale_worker'] = $_SESSION['staff_info']['id'];
		$data['part'] = $_SESSION['staff_info']['part_id'];
	}
	public function _before_delete($option)
	{
		$id = $option['where']['id'];
		if(!empty($id)){
			//删除客户资金操作记录
			$mlmodel= M('MoneyLog');
			$mlmodel->where(array('cus_id'=>array('eq',$id)))
				  ->delete();
			//删除客户出入金分成记录
			$mlmodel= M('MoneyDivide');
			$mlmodel->where(array('cus_id'=>array('eq',$id)))
				  ->delete();
			//删除客户股票操作记录
			$pmodel=M('Position');
			$pmodel->where(array('cus_id'=>array('eq',$id)))
				  ->delete();
			$slmodel=M('StockLog');
			$slmodel->where(array('cus_id'=>array('eq',$id)))
				  ->delete();
			//删除客户沟通记录
			$clmodel=M('CommunicateLog');
			$clmodel->where(array('cus_id'=>array('eq',$id)))
				  ->delete();
		}
	}
	/**
     * 客户出入金总和
     * @author hawk <2271030162@qq.com>
     * @param  $id int
     * @return array
     */
	public function getCustomerMoney($id,$where=array()){
		$model = M('MoneyLog');
		$where['cus_id']=$id;
		$money1 = $model->field('OpFare')->where($where)->select();
		foreach ($money1 as $k => $v) {
			$money_all[] = $v['opfare']; 
		}
		return $money_all;
	}
	/**
     * 业务员业绩
     * @author hawk <2271030162@qq.com>
     * @param  $$where array
     * @return array
     */
	public function getSaleMoney($where=array()){
		$model = M('MoneyDivide');	
		$money = $model->field('OpFare,percent')->where($where)->select();
		foreach ($money as $k => $v) {
			$money_all[] = $v['opfare']*$v['percent']; 
		}
		return $money_all;
	}
}